//
//  SIEmoticonToolbar.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/13.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import UIKit

@objc protocol SIEmoticonToolbarDelegate: NSObjectProtocol {
    ///选中的bar
    func emoticonToolbarDidSelectedItemIndex(toolbar: SIEmoticonToolbar, index: Int)
}


class SIEmoticonToolbar: UIView {
    
    weak var delegate: SIEmoticonToolbarDelegate?

    var selectedIndex: Int = 0 {
        
        didSet {
            
            for btn in subviews as! [UIButton] {
                
                btn.isSelected = false
            }
            
            (subviews[selectedIndex] as! UIButton).isSelected = true
            
        }
        
    }
    
    override func awakeFromNib() {
        
        setupUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let count = subviews.count
        let w = bounds.width / CGFloat(count)
        let rect = CGRect(x: 0, y: 0, width: w, height: bounds.height)
        
        for (i, btn) in subviews.enumerated() {
            
            btn.frame = rect.offsetBy(dx: CGFloat(i) * w, dy: 0)
            
        }
    }
    
    // MARK: - 底部bar监听方法
    @objc private func clickItem(button: UIButton) {
        
        delegate?.emoticonToolbarDidSelectedItemIndex(toolbar: self, index: button.tag)
    }

}

private extension SIEmoticonToolbar {
    
    func setupUI() {
        
        let manager = SIEmotionManager.shared
        
        for (i, p) in manager.packages.enumerated() {
            
            let btn = UIButton()
            
            btn.setTitle(p.groupName, for: [])
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            
            btn.setTitleColor(UIColor.white(), for: [])
            btn.setTitleColor(UIColor.darkGray(), for: .highlighted)
            btn.setTitleColor(UIColor.darkGray(), for: .selected)
            btn.setBackgroundImage(#imageLiteral(resourceName: "compose_toolbar_background"), for: .highlighted)
            
//            btn.backgroundColor = UIColor.cyan()
            // 设置按钮的背景图片
            let imageName = "compose_emotion_table_\(p.bgImageName ?? "")_normal"
            let imageNameHL = "compose_emotion_table_\(p.bgImageName ?? "")_selected"
            
            var image = UIImage(named: imageName, in: manager.bundle, compatibleWith: nil)
            var imageHL = UIImage(named: imageNameHL, in: manager.bundle, compatibleWith: nil)
            
            // 拉伸图像
            let size = image?.size ?? CGSize()
            let inset = UIEdgeInsets(top: size.height * 0.5,
                                     left: size.width * 0.5,
                                     bottom: size.height * 0.5,
                                     right: size.width * 0.5)
            
            image = image?.resizableImage(withCapInsets: inset)
            imageHL = imageHL?.resizableImage(withCapInsets: inset)

            
            btn.sizeToFit()
            
            addSubview(btn)
            
            btn.tag = i
            
            btn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
            
        }
        
        (subviews[0] as! UIButton).isSelected = true
    }
    
}
